/**
 * Copyright 1999-2014 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.roytrack.kryo.factory;


import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.serializers.DefaultSerializers;
import com.roytrack.kryo.nativejava.SerializableClassRegistry;
import de.javakaffee.kryoserializers.*;

import java.lang.reflect.InvocationHandler;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

/**
 * @author lishen
 */
public abstract class KryoFactory {

  //    private static final KryoFactory factory = new PrototypeKryoFactory();
//    private static final KryoFactory factory = new SingletonKryoFactory();
  private static final KryoFactory factory = new PooledKryoFactory();

  private final Set<Class> registrations = new LinkedHashSet<Class>();

  private boolean registrationRequired;

  private volatile boolean kryoCreated;

  protected KryoFactory() {
    // TODO configurable
//        Log.DEBUG();
  }

  public static KryoFactory getDefaultFactory() {
    return factory;
  }

  /**
   * only supposed to be called at startup time
   * <p>
   * later may consider adding support for custom serializer, custom id, etc
   */
  public void registerClass(Class clazz) {

    if (kryoCreated) {
      throw new IllegalStateException("Can't register class after creating kryo instance");
    }
    registrations.add(clazz);
  }

  protected Kryo createKryo() {
    if (!kryoCreated) {
      kryoCreated = true;
    }

    Kryo kryo = new CompatibleKryo();

    // TODO
//        kryo.setReferences(false);
    kryo.setRegistrationRequired(registrationRequired);

    kryo.register(Arrays.asList("").getClass(), new ArraysAsListSerializer());
    kryo.register(GregorianCalendar.class, new GregorianCalendarSerializer());
    kryo.register(InvocationHandler.class, new JdkProxySerializer());
    kryo.register(BigDecimal.class, new DefaultSerializers.BigDecimalSerializer());
    kryo.register(BigInteger.class, new DefaultSerializers.BigIntegerSerializer());
    kryo.register(Pattern.class, new RegexSerializer());
    kryo.register(BitSet.class, new BitSetSerializer());
    kryo.register(URI.class, new URISerializer());
    kryo.register(UUID.class, new UUIDSerializer());
    UnmodifiableCollectionsSerializer.registerSerializers(kryo);
    SynchronizedCollectionsSerializer.registerSerializers(kryo);

    // now just added some very common classes
    // TODO optimization
    kryo.register(HashMap.class);
    kryo.register(ArrayList.class);
    kryo.register(LinkedList.class);
    kryo.register(HashSet.class);
    kryo.register(TreeSet.class);
    kryo.register(Hashtable.class);
    kryo.register(Date.class);
    kryo.register(Calendar.class);
    kryo.register(ConcurrentHashMap.class);
    kryo.register(SimpleDateFormat.class);
    kryo.register(GregorianCalendar.class);
    kryo.register(Vector.class);
    kryo.register(BitSet.class);
    kryo.register(StringBuffer.class);
    kryo.register(StringBuilder.class);
    kryo.register(Object.class);
    kryo.register(Object[].class);
    kryo.register(String[].class);
    kryo.register(byte[].class);
    kryo.register(char[].class);
    kryo.register(int[].class);
    kryo.register(float[].class);
    kryo.register(double[].class);

    for (Class clazz : registrations) {
      kryo.register(clazz);
    }

    for (Class clazz : SerializableClassRegistry.getRegisteredClasses()) {
      kryo.register(clazz);
    }

    return kryo;
  }

  public void returnKryo(Kryo kryo) {
    // do nothing by default
  }

  public void setRegistrationRequired(boolean registrationRequired) {
    this.registrationRequired = registrationRequired;
  }

  public void close() {
    // do nothing by default
  }

  public abstract Kryo getKryo();
}
